package com.weita.lightfood.controller;


import com.weita.lightfood.model.entity.Order;
import com.weita.lightfood.model.query.order.GenerateOrderQuery;
import com.weita.lightfood.model.query.order.RefundOrderQuery;
import com.weita.lightfood.model.vo.order.*;
import com.weita.lightfood.service.OrderService;
import com.weita.lightfood.utils.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * @author IT_Monster at 2019/03/27 23:28.
 *
 * 订单管理接口
 */
@Api(tags = "订单管理接口")
@RestController
@RequestMapping("/order")
public class OrderController {
    @Autowired
    private OrderService orderService;

    /**
     * 早餐下单截止时间
     */
    private static final String END_TIME_MORNING = "07:00:00";
    /**
     * 午餐下单截止时间
     */
    private static final String END_TIME_FORENOON = "11:00:00";
    /**
     * 晚餐下单截止时间
     */
    private static final String END_TIME_AFTERNOON = "17:00:00";

    /**
     * 早餐下单
     */
    @ApiOperation("早餐下单接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户id", defaultValue = "0"),
            @ApiImplicitParam(name = "payment", value = "支付方式（0代表支付宝，1代表微信，目前只写支付宝）", defaultValue = "0"),
            @ApiImplicitParam(name = "dishNumber", value = "菜品份数", defaultValue = "0"),
            @ApiImplicitParam(name = "dishes", value = "菜谱id", defaultValue = "0"),
            @ApiImplicitParam(name = "tableNumber", value = "餐具份数", defaultValue = "0"),
            @ApiImplicitParam(name = "grossPrice", value = "总价格", defaultValue = "0"),
            @ApiImplicitParam(name = "way", value = "配送方式（0代表外卖配送，1代表到店自取）", defaultValue = "0"),
            @ApiImplicitParam(name = "addressId", value = "收货地址id", defaultValue = "0")
    })
    @PostMapping("/generateBreakfastOrder")
    public Result<String> generateBreakfastOrder(GenerateOrderQuery generateOrderQuery){
        try {
            return Result.success(orderService.generateBreakfastOrder(generateOrderQuery, END_TIME_MORNING));
        } catch (Exception e) {
            System.out.println("早餐下单出错");
            return Result.fail("早餐下单失败");
        }
    }

    /**
     * 午餐下单
     */
    @ApiOperation("午餐下单接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户id", defaultValue = "0"),
            @ApiImplicitParam(name = "payment", value = "支付方式（0代表支付宝，1代表微信，目前只写支付宝）", defaultValue = "0"),
            @ApiImplicitParam(name = "dishNumber", value = "菜品份数", defaultValue = "0"),
            @ApiImplicitParam(name = "dishes", value = "菜谱id", defaultValue = "0"),
            @ApiImplicitParam(name = "tableNumber", value = "餐具份数", defaultValue = "0"),
            @ApiImplicitParam(name = "grossPrice", value = "总价格", defaultValue = "0"),
            @ApiImplicitParam(name = "way", value = "配送方式（0代表外卖配送，1代表到店自取）", defaultValue = "0"),
            @ApiImplicitParam(name = "addressId", value = "收货地址id", defaultValue = "0")
    })
    @PostMapping("/generateLunchOrder")
    public Result<String> generateLunchOrder(GenerateOrderQuery generateOrderQuery){
        try {
            return Result.success(orderService.generateLunchOrder(generateOrderQuery, END_TIME_FORENOON));
        } catch (Exception e) {
            System.out.println("午餐下单出错");
            return Result.fail("午餐下单失败");
        }
    }

    /**
     * 晚餐下单
     */
    @ApiOperation("晚餐下单接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户id", defaultValue = "0"),
            @ApiImplicitParam(name = "payment", value = "支付方式（0代表支付宝，1代表微信，目前只写支付宝）", defaultValue = "0"),
            @ApiImplicitParam(name = "dishNumber", value = "菜品份数", defaultValue = "0"),
            @ApiImplicitParam(name = "dishes", value = "菜谱id", defaultValue = "0"),
            @ApiImplicitParam(name = "tableNumber", value = "餐具份数", defaultValue = "0"),
            @ApiImplicitParam(name = "grossPrice", value = "总价格", defaultValue = "0"),
            @ApiImplicitParam(name = "way", value = "配送方式（0代表外卖配送，1代表到店自取）", defaultValue = "0"),
            @ApiImplicitParam(name = "addressId", value = "收货地址id", defaultValue = "0")
    })
    @PostMapping("/generateDinnerOrder")
    public Result<String> generateDinnerOrder(GenerateOrderQuery generateOrderQuery) {
        try {
            return Result.success(orderService.generateDinnerOrder(generateOrderQuery, END_TIME_AFTERNOON));
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("晚餐下单出错");
            return Result.fail("晚餐下单失败");
        }
    }


    /**
     * 用户获取退款订单
     */
    @ApiOperation("用户获取退款订单接口")
    @ApiImplicitParam(name = "userId", value = "用户id", defaultValue = "0")
    @PostMapping("/getTKOrder")
    public Result<List<OrderVo>> getTKOrder(int userId){
        try {
            return Result.success(orderService.getTKOrder(userId));
        } catch (Exception e) {
            System.out.println("用户获取退款订单接口出错");
            return Result.fail("用户获取退款订单失败");
        }
    }

    /**
     * 用户获取全部订单
     */
    @ApiOperation("用户获取全部订单接口")
    @ApiImplicitParam(name = "userId", value = "用户id", defaultValue = "0")
    @PostMapping("/getAllOrder")
    public Result<List<OrderVo>> getAll(int userId) {
        try {
            return Result.success(orderService.getAll(userId));
        } catch (Exception e) {
            System.out.println("用户获取全部订单出错");
            return Result.fail("用户获取全部订单失败");
        }
    }


    /**
     * 用户确认收货
     */
    @ApiOperation("用户确认收货接口")
    @ApiImplicitParam(name = "orderId", value = "订单id", defaultValue = "0")
    @PostMapping("/confirmReceipt")
    public Result<String> confirmReceipt(int orderId) {
        try {
            return Result.success(orderService.confirmReceipt(orderId));
        } catch (Exception e) {
            System.out.println("用户确认收货接口出错");
            return Result.fail("用户确认收货失败");
        }
    }

    /**
     * 商家获取所有已接单订单0
     * @return
     */
    @ApiOperation("商家获取所有已接单订单接口")
    @PostMapping("/getReceivingOrder")
    public Result<Order> getReceivingOrder(){
        try {
            return Result.success(orderService.getReceivingOrder());
        } catch (Exception e) {
            System.out.println("商家获取所有已接单订单接口出错");
            return Result.fail("商家获取所有已接单订单失败");
        }
    }

    /**
     * 商家获取退款申请订单3
     * @return
     */
    @ApiOperation("商家获取退款申请订单接口")
    @PostMapping("/getAllTKSQOrder")
    public Result<List<RefundOrderVo>> getAllTKSQOrder() {
        try {
            return Result.success(orderService.getAllTKSQOrder());
        } catch (Exception e) {
            System.out.println("商家获取退款申请订单接口出错");
            return Result.fail("商家获取退款申请订单失败");
        }
    }

    /**
     * 商家获取已完成订单1245
     * @return
     */
    @ApiOperation("商家获取已完成订单接口")
    @PostMapping("/getAllCGOrder")
    public Result<Order> getAllCGOrder(){
        try {
            return Result.success(orderService.getAllCGOrder());
        } catch (Exception e) {
            System.out.println("商家获取已完成订单接口出错");
            return Result.fail("商家获取已完成订单失败");
        }
    }


    /**
     * 商家获取订单详情
     * @param orderId
     * @return
     */
    @ApiOperation("商家获取订单详情接口")
    @ApiImplicitParam(name = "orderId", value = "订单id", defaultValue = "0")
    @PostMapping("/orderDetails")
    public Result<OrderDetailsVo> orderDetails(int orderId) {
        try {
            return Result.success(orderService.orderDetails(orderId));
        } catch (Exception e) {
            System.out.println("商家获取订单详情接口出错");
            return Result.fail("商家获取订单详情失败");
        }
    }

    /**
     * 用户查看订单详情
     * @param orderId
     * @return
     */
    @ApiOperation("用户查看订单详情接口")
    @ApiImplicitParam(name = "orderId", value = "订单id", defaultValue = "0")
    @PostMapping("/userOrderDetails")
    public Result<UserOrderDetailsVo> userOrderDetails(int orderId){
        try {
            return Result.success(orderService.userOrderDetails(orderId));
        } catch (Exception e) {
            System.out.println("用户查看订单详情接口出错");
            return Result.fail("用户查看订单详情接失败");
        }
    }

    /**
     * 获取退款订单详情
     * @param orderId
     * @return
     */
    @ApiOperation("获取退款订单详情接口")
    @ApiImplicitParam(name = "orderId", value = "订单id", defaultValue = "0")
    @PostMapping("/refundOrderDetails")
    public Result<RefundOrderDetailsVo> refundOrderDetails(int orderId){
        try {
            return Result.success(orderService.refundOrderDetails(orderId));
        } catch (Exception e) {
            System.out.println("获取退款订单详情接口");
            return Result.fail("获取退款订单详情失败");
        }
    }


    /**
     * 用户申请取消订单(参数：订单id和退款理由)
     * @param refundOrderQuery
     * @return
     */
    @ApiOperation("用户申请取消订单接口")
    @ApiImplicitParam(name = "orderId", value = "订单id", defaultValue = "0")
    @PostMapping("/applicationForDrawback")
    public Result<String> applicationForDrawback(RefundOrderQuery refundOrderQuery){
        try {
            return Result.success(orderService.applicationForDrawback(refundOrderQuery));
        } catch (Exception e) {
            System.out.println("用户申请取消订单接口出错");
            return Result.fail("用户申请取消订单接口");
        }
    }

    /**
     * 商家同意取消订单
     * @param orderId
     * @return
     */
    @ApiOperation("商家同意取消订单接口")
    @ApiImplicitParam(name = "orderId", value = "订单id", defaultValue = "0")
    @PostMapping("/agreeDrawback")
    public Result<String> agreeDrawback(int orderId){
        try {
            return Result.success(orderService.agreeDrawback(orderId));
        } catch (Exception e) {
            System.out.println("商家同意取消订单接口出错");
            return Result.fail("商家同意取消订单接口失败");
        }
    }

    /**
     * 商家拒绝取消订单
     * @param orderId
     * @return
     */
    @ApiOperation("商家拒绝取消订单接口")
    @ApiImplicitParam(name = "orderId", value = "订单id", defaultValue = "0")
    @PostMapping("/refuseDrawback")
    public Result<String> refuseDrawback(int orderId){
        try {
            return Result.success(orderService.refuseDrawback(orderId));
        } catch (Exception e) {
            System.out.println("商家拒绝取消订单接口出错");
            return Result.fail("商家拒绝取消订单出错");
        }
    }


    /**
     * 商家获取当月总数据
     * @return
     */
    @ApiOperation("商家获取当月总数据接口")
    @PostMapping("/getThisMonthTotalData")
    public Result<ThisMonthTotalData> getThisMonthTotalData() {
        try {
            return Result.success(orderService.getThisMonthTotalData());
        } catch (Exception e) {
            System.out.println("商家获取当月总数据接口出错");
            return Result.fail("商家获取当月总数据失败");
        }
    }

    /**
     * 商家配送
     * @param orderId
     * @return
     */
    @ApiOperation("商家配送接口")
    @ApiImplicitParam(name = "orderId", value = "订单id", defaultValue = "0")
    @PostMapping("/distributionOrder")
    public Result<String> distributionOrder(int orderId){
        try {
            return Result.success(orderService.distributionOrder(orderId));
        } catch (Exception e) {
            System.out.println("商家配送接口出错");
            return Result.fail("商家配送失败");
        }
    }

    /**
     * 商家查看本月我的账单
     * @return
     */
    @ApiOperation("商家查看本月我的账单接口")
    @PostMapping("/myOrders")
    public Result<Map> myOrders(){
        try {
            return Result.success(orderService.myOrders());
        } catch (Exception e) {
            System.out.println("商家查看本月我的账单接口出错");
            return Result.fail("商家查看本月我的账单接口失败");
        }
    }

    /**
     * 商家查看本月我的退款订单
     * @return
     */
    @ApiOperation("商家查看本月我的退款订单接口")
    @PostMapping("/myRefundOrders")
    public Result<List<MerchantOrder>> myRefundOrders(){
        try {
            return Result.success(orderService.myRefundOrders());
        } catch (Exception e) {
            System.out.println("商家查看本月我的退款订单接口出错");
            return Result.fail("商家查看本月我的退款订单失败");
        }
    }

}
